import { defineStore } from 'pinia';

export const useGraphData = defineStore('graph-data', {
  state: () => ({
    data: [],
    currentId: null,
  }),
  actions: {
    setData(v) {
      const findIndex = this.data.findIndex((item) => item.id === this.currentId);
      if (findIndex > -1) {
        return (this.data[findIndex].cells = v.cells);
      }
      const obj = {
        ...v,
        name: '数据' + Number(this.data.length + 1),
        id: Number(this.data.length + 1),
      };
      this.currentId = obj.id;
      this.data.push(obj);
    },
    delData(id) {
      const findIndex = this.data.findIndex((item) => item.id === id);
      if (findIndex > -1) {
        this.data.splice(i, 1);
      }
    },
    setCurrentId(id) {
      this.currentId = id;
    },
    clearCurrentId() {
      this.currentId = null;
    },
  },
});
